package org.tradeinterceptor.modular.strategy.modules;

import org.tradeinterceptor.modular.strategy.ForexSignal;
import org.tradeinterceptor.modular.strategy.StrategyModule;

import com.riflexo.charting.api.AbstractEasyCalculator;
import com.riflexo.charting.api.StockIndicator;
import com.riflexo.charting.api.StockIndicatorUtil;


public class BollingerBandStrategyModule implements StrategyModule {

	private StockIndicator<Object> BOLLINGER;
	AbstractEasyCalculator parent;
	
	double previousUpper = 0;
	double previousLower = 0;
	
	int period;
	double k;
	
	
	public BollingerBandStrategyModule(int period, double k) {
		// TODO Auto-generated constructor stub
		this.period = period;
		this.k = k;
	}
	
	@Override
	public ForexSignal getSignal() {
		// TODO Auto-generated method stub
		double lower = BOLLINGER.get(StockIndicatorUtil.BollingerBandsChannels.LOWER_BAND.toString());
		double middle = BOLLINGER.get(StockIndicatorUtil.BollingerBandsChannels.MIDDLE_BAND.toString());
		double upper = BOLLINGER.get(StockIndicatorUtil.BollingerBandsChannels.UPPER_BAND.toString());
		
		Double price = parent.getMainSeries().getDataItem().getClose();
		
		ForexSignal signal = ForexSignal.NONE;
		if((upper-lower >= previousUpper - previousLower)){
			signal = ForexSignal.BUY;
		} else if((upper-lower >= previousUpper - previousLower)){
			signal = ForexSignal.SELL;
		}
		
		previousUpper = upper;
		previousLower = lower;
		
		return signal;
	}
	
	@Override
	public void setPosition(ForexSignal signal) {
		// non positional indicator
	}

	@Override
	public void declare(AbstractEasyCalculator parent) {
		// TODO Auto-generated method stub
		
		BOLLINGER = parent.getStockIndicatorUtil().declareBollingerBands("bollinger", period, k);
		this.parent = parent;

	}
	
	@Override
	public void close() {
		// TODO Auto-generated method stub
		
	}

}
